package com.neuedu.javase.tools.date;

import java.util.Calendar;

/***
 * 项目名称:  MyJavaSE 
 * 包：  com.neuedu.javase.tools.date 
 * date:   2025/10/30   13:34  
 * 描述:  
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class MyCalendar {

    public static void main(String[] args) {
        // Calendar
        //创建对象  默认是  GregorianCalendar 公立
        Calendar calendar = Calendar.getInstance(); //默认当前时区的时间

        System.out.println("calendar.getClass() = " + calendar.getClass());
        System.out.println("calendar = " + calendar);



    //     获取字段
        int year = calendar.get(Calendar.YEAR);
        //月份从0 开始  0--11
        int month = calendar.get(Calendar.MONTH) + 1;
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        int hour = calendar.get(Calendar.HOUR);   //12 小时
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);  //24 时

        // CTRL+SHIFT+ U 切换大小写
        int minutes = calendar.get(Calendar.MINUTE);  //24 时
        int seconds = calendar.get(Calendar.SECOND);  //24 时

        // yyyy-MM-dd HH:mm:ss  星期xx

        System.out.println("获取字段1 : "+year+"-"+month+"-"+dayOfMonth+" "+hour +":"+minutes+":"+seconds+"  星期"+dayOfWeek+"");
        System.out.println("获取字段2 : "+year+"-"+month+"-"+dayOfMonth+" "+hourOfDay +":"+minutes+":"+seconds+"  星期"+dayOfWeek+"");

        System.out.println("一年中的第几天 dayOfYear = " + dayOfYear);
        System.out.println("一年中的第几天 dayOfYear = " + dayOfYear+"/365");



        //设置年月日
        Calendar now = Calendar.getInstance();//
        System.out.println("=================now=====================");

        formatOutput(now);

        //设置时间

        //public void set(int field, int value)
        // set(int year, int month, int date)
        //set(int year, int month, int date, int hourOfDay, int minute )
        //set(int year, int month, int date, int hourOfDay, int minute,     int second)
        now.set(2024,  12-1,  15, 12,13,14);
        System.out.println("=================now.set(2024,  12-1,  15, 12,13,14)=====================");
        formatOutput(now);

        System.out.println("============= now.set(Calendar.YEAR,  2028);============================");
        now.set(Calendar.YEAR,  2028);
        formatOutput(now);





        Calendar now2 = Calendar.getInstance();
        System.out.println("=================now2=============== ");
        formatOutput(now2);

        now2.add(Calendar.MINUTE, 59);
        System.out.println("====================now2.add(Calendar.MINUTE, 59);=============== ");
        formatOutput(now2);


        now2.add(Calendar.MINUTE, -59);
        System.out.println("====================now2.add(Calendar.MINUTE, -59);;=============== ");
        formatOutput(now2);














    }


    /**
     * 格式化输出
     * @param calendar
     * @return
     */
    public static  void  formatOutput(Calendar calendar){
        //     获取字段
        int year = calendar.get(Calendar.YEAR);
        //月份从0 开始  0--11
        int month = calendar.get(Calendar.MONTH) + 1;
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        int hour = calendar.get(Calendar.HOUR);   //12 小时
        int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY);  //24 时

        // CTRL+SHIFT+ U 切换大小写
        int minutes = calendar.get(Calendar.MINUTE);  //24 时
        int seconds = calendar.get(Calendar.SECOND);  //24 时

        String format =  year+"-"+month+"-"+dayOfMonth+" "+hourOfDay +":"+minutes+":"+seconds+"  星期"+dayOfWeek;
        System.out.println(format);

    }
}
